package com.jie.eclipse.pan.editors;

import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;

import com.jie.eclipse.pan.action.BaseAction;

/**
 * Manages the installation/deinstallation of global actions for multi-page editors.
 * Responsible for the redirection of global actions to the active editor.
 * Multi-page contributor replaces the contributors for the individual editors in the multi-page editor.
 */
public class PLLFormEditorContributor extends MultiPageEditorActionBarContributor {
	private IEditorPart activeEditorPart;
	
	private Action sampleAction;
	private IAction open0;
	private IAction open00;
	private IAction open1;
	private IAction open2;
	private IAction open3;
	private IAction open4;
	private IAction open5;
	private IAction open_O;
	/**
	 * Creates a multi-page contributor.
	 */
	public PLLFormEditorContributor() {
		super();
		createActions();
	}
	
	/**
	 * Returns the action registed with the given text editor.
	 * @return IAction or null if editor is null.
	 */
	protected IAction getAction(ITextEditor editor, String actionID) {
		return (editor == null ? null : editor.getAction(actionID));
	}
	
	
	public void setActivePage(IEditorPart part) {
		if (activeEditorPart == part)
			return;

		activeEditorPart = part;
		
		IActionBars actionBars = getActionBars();
		if (actionBars != null) {
			ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null;
			actionBars.setGlobalActionHandler(
				ActionFactory.DELETE.getId(),
				getAction(editor, ITextEditorActionConstants.DELETE));
			actionBars.setGlobalActionHandler(
				ActionFactory.UNDO.getId(),
				getAction(editor, ITextEditorActionConstants.UNDO));
			actionBars.setGlobalActionHandler(
				ActionFactory.REDO.getId(),
				getAction(editor, ITextEditorActionConstants.REDO));
			actionBars.setGlobalActionHandler(
				ActionFactory.CUT.getId(),
				getAction(editor, ITextEditorActionConstants.CUT));
			actionBars.setGlobalActionHandler(
				ActionFactory.COPY.getId(),
				getAction(editor, ITextEditorActionConstants.COPY));
			actionBars.setGlobalActionHandler(
				ActionFactory.PASTE.getId(),
				getAction(editor, ITextEditorActionConstants.PASTE));
			actionBars.setGlobalActionHandler(
				ActionFactory.SELECT_ALL.getId(),
				getAction(editor, ITextEditorActionConstants.SELECT_ALL));
			actionBars.setGlobalActionHandler(
				ActionFactory.FIND.getId(),
				getAction(editor, ITextEditorActionConstants.FIND));
			actionBars.setGlobalActionHandler(
				IDEActionFactory.BOOKMARK.getId(),
				getAction(editor, IDEActionFactory.BOOKMARK.getId()));
			actionBars.updateActionBars();
		}
	}
	
	
	@Override
	public void init(IActionBars actionBars, IWorkbenchPage page) {
		super.init(actionBars, page);
		if (actionBars != null) {
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open00action", open00);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open0action", open0);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open1action", open1);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open2action", open2);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open3action", open3);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open4action", open4);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.open5action", open5);
			actionBars.setGlobalActionHandler("com.jie.eclipse.pan.action.openOaction", open_O);
		}
	}
	
	private void createActions() {
		sampleAction = new Action() {
			public void run() {
				MessageDialog.openInformation(null, "Pan", "Sample Action Executed");
			}
		};
		sampleAction.setText("Sample Action");
		sampleAction.setToolTipText("Sample Action tool tip");
		sampleAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK));
		open00 = new BaseAction(this,"altshift`");
		open0 = new BaseAction(this,"alt`");
		open1 = new BaseAction(this,"alt1");
		open2 = new BaseAction(this,"alt2");
		open3 = new BaseAction(this,"alt3");
		open4 = new BaseAction(this,"alt4");
		open5 = new BaseAction(this,"alt5");
		open_O = new BaseAction(this,"alt_O");
	}
	public void contributeToMenu(IMenuManager manager) {
		IMenuManager menu = new MenuManager("Editor &Menu");
		manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu);
		menu.add(sampleAction);
	}
	public void contributeToToolBar(IToolBarManager manager) {
		manager.add(new Separator());
		manager.add(sampleAction);
	}
	public IEditorPart getActiveEditorPart() {
		return activeEditorPart;
	}
}
